package com.zlq.cn.service.un;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import com.zlq.cn.entity.ZhCommonWords;
import com.zlq.cn.mapper.un.ZhCommonWordsMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlq.cn.util.StringUtils;
import lombok.SneakyThrows;
import okhttp3.HttpUrl;
import org.apache.commons.io.FilenameUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 藏文常用词 服务实现类
 * </p>
 *
 * @author zhoucl
 * @since 2024-05-06
 */
@Service
public class ZhCommonWordsService extends ServiceImpl<ZhCommonWordsMapper, ZhCommonWords> {
    @Resource
    private ConfigService configService;

    @SneakyThrows
    public void pull() {
        List<ZhCommonWords> list = super.list();
        for (ZhCommonWords zhCommonWords : list) {
            List<String> pathSegments = HttpUrl.get(zhCommonWords.getSourceUrl()).pathSegments();
            String s1 = pathSegments.get(pathSegments.size() - 2);
            String s2 = pathSegments.get(pathSegments.size() - 1);
            String path = "C:\\Users\\14988\\Desktop\\mp4\\zy\\" + s1 + "_" + s2;
            path = path.replace(".swf", ".mp3");
            String name = "common_words/" + FilenameUtils.getName(path);
            zhCommonWords.setUrl(configService.putObject(name, new FileInputStream(path)));
        }
        super.updateBatchById(list);
//        List<ZhCommonWords> zhCommonWordsList = new ArrayList<>();
//        String baseUrl = "https://buddhism.lib.ntu.edu.tw/test_teach/INFORMAL2";
//        for (int i = 0; i < 6; i++) {
//            String url = baseUrl + ".HTM";
//            if (i > 0) {
//                url = baseUrl + "-" + i + ".HTM ";
//            }
////        Document document = Jsoup.parse(FileUtil.readString(new File("C:\\Users\\14988\\Desktop\\zuowen.txt"), CharsetUtil.CHARSET_UTF_8));
//            Document document = Jsoup.connect(url).get();
//            Elements elements = document.selectFirst("table").select("tr");
//            for (Element element : elements) {
//                ZhCommonWords zhCommonWords = new ZhCommonWords();
//                String attr = element.child(0).selectFirst("embed").attr("src");
//                String font = element.child(1).text();
//                String sourceUrl = "https://buddhism.lib.ntu.edu.tw/test_teach/" + attr;
//                zhCommonWords.setSourceUrl(sourceUrl);
//                List<String> pathSegments = HttpUrl.get(sourceUrl).pathSegments();
//                String s1 = pathSegments.get(pathSegments.size() - 2);
//                String s2 = pathSegments.get(pathSegments.size() - 1);
//                HttpUtil.downloadFile(sourceUrl, new File("C:\\Users\\14988\\Desktop\\mp4\\zy\\" + s1 + "_" + s2));
////            zhCommonWords.setTibetan();
//                zhCommonWords.setZhCn(font);
//                zhCommonWordsList.add(zhCommonWords);
//            }
//        }
//        super.saveBatch(zhCommonWordsList);
    }

    public static void main(String[] args) {
//        new ZhCommonWordsService().pull();
        String str = "https://buddhism.lib.ntu.edu.tw/test_teach/usually-flash/24.swf";
        List<String> strings = HttpUrl.get(str).pathSegments();
        System.out.println(strings);
    }
}
